import operator from '@/constant/operator.json'
import software from '@/constant/software.json'
import type { IAnsweredQuestion, IChapter, ICourse, IQuestion, IAnsweredCourse } from './types'

const parseRawCourse = (title: string, RowCourse: any) => {
  let course: ICourse = {
    title,
    chapters: []
  }
  for (const key in RowCourse) {
    const element = RowCourse[key]
    course.chapters.push(element)
  }

  return course
}

const generatorAnsweredCourse = (courses: ICourse[], get: any) => {
  let list: IAnsweredCourse[] = []
  courses.forEach((courses) => {
    list.push({
      id: courses.title,
      questions: get(courses.title) || []
    })
  })

  return list
}

export const useStore = () => {
  const localStore = useLocalStor()
  const operatorCourse = parseRawCourse('操作系统', operator)
  const softwareCourse = parseRawCourse('软件工程', software)
  const courses: ICourse[] = [operatorCourse, softwareCourse]
  const answeredCourse: IAnsweredCourse[] = generatorAnsweredCourse(courses, localStore.get)
  const answeredQuestions: IAnsweredQuestion[] = localStore.get('操作系统') || []

  const getOriginalCourse = (id: string) => {
    return courses.find((course) => course.title === id)
  }

  const getAnswerCourse = (id: string) => {
    return answeredCourse.find((item) => item.id === id)
  }

  const getCourse = (id: string) => {
    const course = getOriginalCourse(id)
    const originalChapters = course?.chapters
    const restChapter = getRestChapter(id)

    if (originalChapters) {
      restChapter.push(...originalChapters)
      course.chapters = restChapter
    }

    return course
  }

  const getChapter = (courseId: string, chapterId: string) => {
    const course = getOriginalCourse(courseId)
    if (!course) return null

    return course.chapters.find((chapter) => chapter.title === chapterId)
  }

  const getRestChapter = (courseId: string) => {
    const resetQuestion: IChapter[] = []
    const { unAnsweredQuestions, answeredErrorQuestions } = getAnswerCategories(courseId)
    resetQuestion.push({
      title: '未回答的题目',
      questions: unAnsweredQuestions
    })

    resetQuestion.push({
      title: '错误的题目',
      questions: answeredErrorQuestions
    })
    return resetQuestion
  }

  const getAnswerCategories = (courseId: string) => {
    const cache: Record<string, IAnsweredQuestion> = {}
    const answeredErrorQuestions: IQuestion[] = []
    const unAnsweredQuestions: IQuestion[] = []
    const answerCourse = getAnswerCourse(courseId)
    answerCourse?.questions.forEach((item) => {
      cache[item.id] = item
    })

    const course = getOriginalCourse(courseId)!

    course.chapters.forEach((chapter) => {
      chapter.questions.forEach((question) => {
        const answered = cache[question.description]
        answered
          ? answered.error && answeredErrorQuestions.push(question)
          : unAnsweredQuestions.push(question)
      })
    })

    return {
      answeredErrorQuestions,
      unAnsweredQuestions
    }
  }

  return {
    courses,
    getCourse,
    getChapter
  }
}

export const useLocalStor = () => {
  const set = (key: string, value: any) => {
    const json = typeof value === 'object' ? JSON.stringify(value) : value
    localStorage.setItem(key, json)
  }

  const get = (key: string) => {
    const json = localStorage.getItem(key)
    const data = json ? JSON.parse(json) : json

    return data
  }

  return {
    set,
    get
  }
}
